package Thread;
//使用Thread自带的终止标志位
public class Demo9 {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            //此处的Thread.currentThread()就是t,但是lambda表达式是在构造t之前就定义好了的，编译器看到的lambda里的t就会认为这是一个还没有初始化的变量
            while (!Thread.currentThread().isInterrupted()){
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        });
        t.start();
        Thread.sleep(3000);
        //将终止标志位设置为true，终止线程t
        t.interrupt();
    }
}
